package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;

import entity.User;
import utils.DBHelper;
import utils.SendMailUtil;

public class FindPasswordDao {
	private FindPasswordDao() {
	}

	public static FindPasswordDao getInstance() {
		return new FindPasswordDao();
	}
	
	/**
	 * 根据用户输入的旧密码和新密码来修改用户的密码
	 * @param user_id
	 * @param oldPassword 用户输入的旧密码
	 * @param newPassword 用户想要改成的新密码
	 * @return 用户若输入的旧密码不正确，则返回false修改失败
	 */
	public boolean modifyPassword(int user_id,String oldPassword,String newPassword) {
		String old="";
		try {
			Connection c = DBHelper.getInstance().getConnection();
			Statement s = c.createStatement();
			String sql = "select user_password from user where user_id = " + user_id;
			ResultSet rs = s.executeQuery(sql);
			if(rs.next()) {
				old=rs.getString("user_password");	
			}
			if(oldPassword.equals(old)) {//如果输入的旧密码正确，更新密码
				String sql2 = "update user set user_password = ? where user_id = " + user_id;
				PreparedStatement ps = c.prepareStatement(sql2);
				ps.setString(1, newPassword);
				ps.execute();
				return true;
			}
			DBHelper.closeConnection(c, s, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	
	
	
	/**
	 * 获取数据库中保存的验证码
	 * @param user_id
	 * @return
	 */
	public String getCode(int user_id) {
		
		String code=null;
		try {			
			Connection c = DBHelper.getInstance().getConnection();
			Statement s = c.createStatement();
			String sql = "select code from verification_code where user_id = " + user_id;
			ResultSet rs = s.executeQuery(sql);
			if(rs.next()) {
				code=rs.getString("code");
				
				DBHelper.closeConnection(c, s, rs);
			}else {
				code="none";//如果查询结果为空，则返回"none"字符串
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return code;
		
	}
	/**
	 * 更新密码，并删除数据库中的验证码
	 * @param user_id
	 * @param newPassword
	 * @return
	 */
	public boolean updatePassword(int user_id, String newPassword) {
		try {
			Connection c = DBHelper.getInstance().getConnection();
			
			String sql = "update user set user_password = ? where user_id = " + user_id;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setString(1, newPassword);

			ps.execute();
			

			Statement s = c.createStatement();

			String sql2 = "delete from verification_code where user_id = " + user_id;

			s.execute(sql2);
			
			DBHelper.closeConnection(c, ps, null);
			return true;

		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return false;//若返回了false，说明更新没有执行成功
	}
	
	/**
	 * 产生验证码，并存入到数据库
	 * @param user_id
	 */
	public void processCode(int user_id) {
		if(getCode(user_id).equals("none")) {
			int codes[] = rand(0,9,4);
			String gencode = "";
			gencode+=codes[0];
			gencode+=codes[1];
			gencode+=codes[2];
			gencode+=codes[3];
			
			try {
				Connection c = DBHelper.getInstance().getConnection();
				String sql = "insert into verification_code values(?,?)";
				
				PreparedStatement ps = c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				ps.setString(1, ""+user_id);
				ps.setString(2, ""+gencode);
				
				ps.execute();
				System.out.println("成功生成验证码\n"+"id:"+user_id+"  code:"+gencode);
				
			}catch(Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	
	
	/**
	 * 向用户id为user_id的用户发送验证码
	 * @param user_id
	 * @return
	 */
	public void sendCodeEmail(int user_id) {
		try {
			Connection c = DBHelper.getInstance().getConnection();
			Statement s = c.createStatement();
			String sql = "select user_email from user where user_id = " + user_id;
			ResultSet rs = s.executeQuery(sql);
			if(rs.next()) {
				String email = rs.getString("user_email");
				SendMailUtil util = new SendMailUtil();
				String subject="找回密码通知";
				String body="重置密码的验证码为："+getCode(user_id)+"\n提示：该验证码请勿向别人透露，以防账号被盗！";
				util.sendMail(email, subject, body);
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 发送邮件
	 * @param user_id 邮件接收方
	 * @param title 邮件标题
	 * @param content 邮件内容
	 */
	public void sendEmailInterface(int user_id,String title,String content) {
		System.out.println(user_id);
		System.out.println(title);
		System.out.println(content);
		try {
			Connection c = DBHelper.getInstance().getConnection();
			Statement s = c.createStatement();
			String sql = "select user_email from user where user_id = " + user_id;
			ResultSet rs = s.executeQuery(sql);
			if(rs.next()) {
				String email = rs.getString("user_email");
				System.out.println(email);
				SendMailUtil util = new SendMailUtil();
				util.sendMail(email, title, content);
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 产生num个随机数，随机数的范围[begin,end],
	 * @param 随机数开始
	 * @param num 一次函数调用产生的随机数的数量
	 * @return 返回一个数组，数组是产生的指定数量的随机数
	 */
	public static int[] rand(int begin,int end,int num) {
		Random rand = new Random();
		int lib[] = new int[num];
		for(int i =0;i<num;i++) {
			lib[i]=rand.nextInt(end-begin+1)+begin;
		}
		return lib;
	}
	
}
